<?php

namespace App\Logic\Dashboard\Form;

use App\Core\BaseListForm;
use App\Model\Member\MemberModel;
use App\Model\Order\OrderModel;
use App\Model\Referrer\ReferrerModel;

class DashboardStatisticsForm extends BaseListForm{

    public function statistics() : array
    {
        $today_start = date('Y-m-d 00:00:00');
        $today_end = date('Y-m-d 23:59:59');
        $where = [];

        /** @var int $countMember 用户数量 */
        $countMember = MemberModel::makeQueryModel($where)->field('COUNT(1) `count`')->get()->count;

        /** @var int $countTodayMember 今日新增用户 */
        $where = [
            'created_at' => [[$today_start, $today_end], 'between']
        ];
        $countTodayMember = MemberModel::makeQueryModel($where)->field('COUNT(1) `count`')->get()->count;

        /** @var int $countVipMember 会员用户-VIP用户 */
        $where = [
            'type' => [MemberModel::TYPE_NORMAL, '>']
        ];
        $countVipMember = MemberModel::makeQueryModel($where)->field('COUNT(1) `count`')->get()->count;

        /** @var double $sumVipOrder 累计会费用 */
        $where = [
            'pay_state' => OrderModel::PAYSTATE_PAID
        ];
        $sumVipOrder = OrderModel::makeQueryModel($where)->field('SUM(pay_amount) `sum`')->get()->sum;

        /** @var int $countTodayVip 今日升级会员 */
        $where = [
            'pay_state' => OrderModel::PAYSTATE_PAID,
            'created_at' => [[$today_start, $today_end], 'between']
        ];
        $countTodayVip = OrderModel::makeQueryModel($where)->field('COUNT(DISTINCT user_id) `count`')->get()->count;

        /** @var double $sumTodayVip 今日会员费用 */
        $sumTodayVip = OrderModel::makeQueryModel($where)->field('SUM(pay_amount) `sum`')->get()->sum;

        /** @var int $countReferrerParent 推广员人数 */
        $where = [
            'referrer_type' => ReferrerModel::REFERRER_MEMBER
        ];
        $countReferrerParent = ReferrerModel::makeQueryModel($where)->field('COUNT(DISTINCT user_id) `count`')->get()->count;

        /** @var int $countReferrerMember 推广注册总数 */
        $where = [
            'referrer_type' => ReferrerModel::REFERRER_MEMBER
        ];
        $countReferrerMember = ReferrerModel::makeQueryModel($where)->field('COUNT(DISTINCT referrer_user_id) `count`')->get()->count;

        /** @var int $countTodayReferrerMember 今日推广注册*/
        $where = [
            'referrer_type' => ReferrerModel::REFERRER_MEMBER,
            'created_at' => [[$today_start, $today_end], 'between']
        ];
        $countTodayReferrerMember = ReferrerModel::makeQueryModel($where)->field('COUNT(DISTINCT referrer_user_id) `count`')->get()->count;

        /** @var int $countTrialMember 试用用户:会员等级为普通会员且VIP状态为正常使用 */
        $where = [
            'type' => MemberModel::TYPE_NORMAL,
            'vip_state' => MemberModel::STATE_NORMAL
        ];
        $countTrialMember = MemberModel::makeQueryModel($where)->field('COUNT(1) `count`')->get()->count;

        $result = [
            'countMember' => $countMember,
            'countVipMember' => $countVipMember,
            'sumVipOrder' => $sumVipOrder ?? '0.00',
            'countReferrerParent' => $countReferrerParent,
            'countReferrerMember' => $countReferrerMember,
            'countTodayMember' => $countTodayMember,
            'countTodayVip' => $countTodayVip,
            'sumTodayVip' => $sumTodayVip ?? '0.00',
            'countTodayReferrerMember' => $countTodayReferrerMember,
            'countTrialMember' => $countTrialMember,
        ];
        return $result;

    }

}
